package com.troubadorian.feeds.customviews;

import com.troubadorian.feeds.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RepeatingVerticalImageView extends ImageView {
	private Context mContext;
	private int viewHeight;
	private Bitmap bgTile;

	public RepeatingVerticalImageView(Context context) {
		super(context);
		mContext = context;
		try {
			bgTile = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zigzag_vertical);
		} catch (Throwable e) {
			bgTile = null;
		}
	}

	public RepeatingVerticalImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		try {
			bgTile = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zigzag_vertical);
		} catch (Throwable e) {
			bgTile = null;
		}
	}

	public RepeatingVerticalImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		try {
			bgTile = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zigzag_vertical);
		} catch (Throwable e) {
			bgTile = null;
		}
	}

	@Override
	protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
		super.onSizeChanged(xNew, yNew, xOld, yOld);

		viewHeight = yNew;
	}

	@Override
	public void onDraw(Canvas canvas) {
		if (bgTile == null)
			return;

		float top = 0;
		float bgTileHeight = bgTile.getHeight();

		while (top < viewHeight) {
			if (top + bgTileHeight <= viewHeight)
				canvas.drawBitmap(bgTile, 0, top, null);
			top += bgTileHeight;
		}
	}
}
